$(function(){
	parent.IndexPage.toggleLocker('close'); // 沉浸式体验
	// 让jquery的contains方法不区分大小写的识别
	$.expr[":"].contains = $.expr.createPseudo(function(arg) {
	    return function( elem ) {
	        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
	    };
	});
});
var BlogPage = {
	'lastBlogType':null,
	// 查询博客分类成功回调
	'findCategorySucc':function(data,status,xhr) {
		// 输出博客分类
		
		// 搜索文章
		/*$("#searchArticle").on('input propertychange',function(e){  // old-20171003
			var $articleList = $(".t-article-list"); // old-20171003
			var val = $(this).val();
			if(StringUtil.isBlank(val)){
				$articleList.find(".t-article").css("display","block");
			}else {
				$articleList.find(".t-article").css("display","none");
				$articleList.find(".t-article a:contains(" + val + ")").each(function(){
					$(this).closest(".t-article").css("display","block");
				});
			}
		});*/
		
		// 搜索文章-支持solr搜索
		$("#searchArticle").on('keydown',function(e){ // new-20171003
			if (e.keyCode == 13) { // new-20171003, Enter键
				var param = $(this).val(); // search text
				searchBlogAjax.tempUrl(function(url) {
					return url + "/" + param;
				});
				searchBlogAjax.send();
			}
			if (e.keyCode == 27) { // Esc键
				$(this).val('');
				searchBlogAjax.send();
			}
		});
		
		// 搜索博客分类
		$("#searchBlogType").bind('input propertychange',function(){
			var val = $(this).val();
			categoryTree.search(val);
		});
	},
//	'searchBlog':function(search){
//		var param = $("#searchArticle").val();
//		searchBlogAjax.tempUrl(function(url) {
//			return url + "/" + param;
//		});
//		searchBlogAjax.send();
//	},
	'searchBlogSucc':function(data, status, xhr){
		$(".t-article-list").article({
			'data':data,
			'handleData':BlogPage.handleData,
			'contentDefine':{ // 定义内容结构
				"title":"blogName",
				"target":"target", // 目标网页，超链接
				"summary":"remarks", // 内容概述 
				"id":"blogId",// 指定id
				"text":"text", // 摘要
				"notes":[
				     {"name":"更新时间","key":"updateTime"},
				     {"name":"作者","key":"author"},
				     {"name":"阅读次数","key":"visits"}
				 ] // 脚注 
			},
			'onItemClick':function(e,item) {
				signReadAjax.tempUrl(function(url){
					return url + "/" + item.blogId;
				});
				signReadAjax.send();
			},
			'optBar':BlogPage.optBar,
			'onRenderItem':BlogPage.onRenderItem
		});
	},
	'onRenderItem':function($article, item) {
		$article.attr('data-blog-attr', item.blogAttr);
	},
	'onSelected':function(node) {
		var value = node['codeInfoValue'];
		BlogPage.lastBlogType = value;
		findBlogByCategoryAjax.tempUrl(function(url) {
			return url + "/" + value;
		});
		findBlogByCategoryAjax.send();
	},
	'uploadArticle':function(){
		var node = categoryTree.getCurrentNode();
		MsgBox.open("blogAdd", "上传博客", ctx + "/datum/blog/blogAdd/index/" + node.codeInfoValue, '570px');
	},
	// 刷新当前博客页面
	'refreshBlogPage':function() {
		findBlogByCategoryAjax.tempUrl(function(url) {
			return url + "/" + BlogPage.lastBlogType;
		}).send();
	},
	// 刷新阅读次数
	'refreshReadCount':function(data,status,xhr) {
		var $readCount = $(".t-article[data-id='" + data.info + "']").find("[data-tag='visits'] .t-note-value");
		var count = parseInt($readCount.text()) + 1;
		$readCount.text(count); 
	},
	// 打开编辑博客类型对话框
	'openEditBlogTypeDialog':function() {
//		MsgBox.open("editBlogType", "修改博客类型", ctx + "/datum/blog/editBlogType/index", '650px');
		MsgBox.open("editBlogType", "修改博客类型", ctx + "/datum/blog/editTreeBlogType/index", '650px');
	},
	// 数据加工
	'handleData':function(data) {
		var list = data.info;
		$.each(list,function(index,item){
			// var fileName = item.fileInfo.fileName;
			var fileName = item.link;
			
			if(StringUtil.isNotBlank(fileName) && (fileName.startsWith("http://") || fileName.startsWith("https://") 
					|| fileName.startsWith("ftp://"))){
				item["target"] = fileName;
			} else {
				item["target"]= blogPath + "/datum/"+item.fileInfo.fileName;
			}
		});
		return data;
	},
	'optBar':function(item) {
		var dom = "<i class='fa fa-fw fa-street-view' title='阅读记录' onclick='BlogPage.getReadBlogPerson(this)'></i>"+
		"<i class='fa fa-fw fa-trash' title='删除' onclick='BlogPage.deleteBlog(this)'></i>" +
		"<i class='fa fa-fw fa-edit' title='修改' onclick='BlogPage.updateBlog(this)'></i>" + 
		"<i class='fa fa-fw fa-qrcode' title='二维码' onclick='BlogPage.viewQrcode(this)'></i>";
		if (item.blogAttr == 1) {
			dom = dom + "<i class='fa fa-fw fa-download' title='下载markdown' onclick='BlogPage.mdDownload(this)'></i>";
		}
		return dom;
	},
	// 根据分类查询博客成功回调
	'findBlogByCategorySucc':function(data, status, xhr) {
		$(".t-article-list").article({
			'data':data,
			'handleData':BlogPage.handleData,
			'contentDefine':{ // 定义内容结构
				"title":"blogName",
				"target":"target", // 目标网页，超链接
				"summary":"remarks", // 内容概述 
				"id":"blogId",// 指定id
				"notes":[
				     {"name":"更新时间","key":"updateTime"},
				     {"name":"作者","key":"author"},
				     {"name":"阅读次数","key":"visits"}
				 ] // 脚注 
			},
			'onItemClick':function(e,item) {
				signReadAjax.tempUrl(function(url){
					return url + "/" + item.blogId;
				});
				signReadAjax.send();
			},
			'optBar':BlogPage.optBar,
			'onRenderItem':BlogPage.onRenderItem
		});
	},
	// 删除博客
	'deleteBlog':function(ele) {
		MsgBox.confirm("确定删除此博客吗？", null, function() {
			var id = $(ele).closest(".t-article").attr("data-id");
			deleteBlogByIdAjax.tempUrl(function(url) {
				return url + "/" + id;
			});
			deleteBlogByIdAjax.send();
		});
	},
	// 删除博客成功
	'deleteBlogSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		BlogPage.refreshBlogPage();
	},
	// 修改博客
	'updateBlog':function(ele) {
		var id = $(ele).closest(".t-article").attr("data-id");
		var blogAttr = $(ele).closest(".t-article").attr("data-blog-attr");
		if (blogAttr == '0') {
			MsgBox.open("updateBlogType", "修改博客信息", ctx + "/datum/blog/blogUpdate/index/" + id, '570px', '600px');
		}
		if (blogAttr == '1') {
			MsgBox.open("mdUpdateBlogType", "修改博客信息", ctx + "/datum/blog/mdBlogUpdate/index/" + id, '100%', null, null, 'comfortable-eye');
		}
	},
	// 修改成功
	'updateBlogSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		BlogPage.refreshBlogPage();
	},
	// 查看二维码
	'viewQrcode':function(ele) {
		var text = $(ele).closest(".t-article").find("a[data-link]").attr("href");
		var cryptText = BASE64.encoder(text);
		MsgBox.open("viewQrcode", "查看二维码", ctx + "/datum/qrcode/" + cryptText, '400px', null, null, 'no-footer');
	},
	//查看最近查看blog的人
	'getReadBlogPerson':function(ele) {
		var id = $(ele).closest(".t-article").attr("data-id");
		MsgBox.open("addProj", "查看阅读记录", ctx + '/datum/blog/read/find/index/' + id, '465px');
	},
	// 撰写文章
	'writeArticle':function() {
		var node = categoryTree.getCurrentNode();
		MsgBox.open("writeArticle", "Markdown编辑博客", ctx + "/datum/blog/mdBlogEdit/" + node.codeInfoValue + "/index", '100%', null, null, 'comfortable-eye');
	},
	// 下载markdown文档
	'mdDownload':function(ele) {
		var id = $(ele).closest(".t-article").attr("data-id");
		$.download({
			'url': ctx + '/datum/blog/mdDownload/' + id,
			'type':'get',
		});
	},
	// 打包markdown
	'packMarkdown':function() {
		// 1.列出最近7天的markdown文档
		MsgBox.open("packMarkdown", "打包Markdown", ctx + "/datum/blog/mdPack/index", "900px");
	}
};
